﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.IO;

using ECCIT.Business.Model;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;

public partial class admin_manager_static_pub_list : AdminBasePage
{
    //public IList datalist = new ArrayList();
    private const string VIEWSTATE_LANG_ID = "langID";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if(!IsPostBack)
        {
            //权限判断
            PopedomValidate(Layerid, QUERY);

            string cid = Request.Params["columnid"];
            //            string webid = Request.Params["webid"];
            ColumnBll cbll = new ColumnBll();
            ColumnModel cm = cbll.GetColumn(int.Parse(cid));
            string layerid = cm.Layerid;
            if (cid != null && cid != ""
                && layerid != null && layerid != ""
                )
            {
                IList tlist = cbll.GetColumnList(int.Parse(cid));
                IList datalist = new ArrayList();
                for (int i = 0; i < tlist.Count; i++)
                {
                    ColumnModel cmtem = (ColumnModel)tlist[i];
                    if (cmtem.Isstaticpub == CmsConstants.YES || cmtem.Class_is_static == CmsConstants.YES)
                    {
                        datalist.Add(cmtem);
                    }
                }
                GridView1.DataSource = datalist;
                GridView1.AllowPaging = false;
                GridView1.DataBind();
                /*for (int i = 0; i < datalist.Count; i++)
                {
                    ColumnModel cmtem = (ColumnModel)datalist[i];
                    if (cmtem.Isstaticpub != CmsConstants.YES)
                    {
                        datalist.RemoveAt(i);
                    }
                }*/

                this.cnavi.Text = cbll.LayeridSysColumnNaiv(Layerid) + cbll.StaticPubLayeridColumnNaiv(layerid, Layerid);
            }
        }
    }

    // 全站生成
    protected void All_Btn_click(object sender, EventArgs e)
    {
        ColumnModel cm = null;
        try
        {
            this.PopedomValidate(Layerid, PUB);
            start();
            int webid = int.Parse(Request.Params["webid"]);
            MenuModel menuModel = new MenuModel();
            IList datalist = menuModel.GetLayeridColumn(webid);
            datalist.Add(menuModel.GetColumn(webid));
            CmsArticleBll acbll = new CmsArticleBll();
            List<LuceneSearchDoc> doclist = new List<LuceneSearchDoc>();
            for (int i = 0; i < datalist.Count; i++)
            {
                cm = (ColumnModel)datalist[i];
                string dir = menuModel.GetLayerDir(cm.Layerid);
                string chtml = acbll.CreateColumnIndexHtml(cm, dir);

                if (cm.Isstaticpub == CmsConstants.YES)
                {
                    GenerateHtmlByColumnID(cm.Classid);
                }
            }

            IndexFiles();
            end();
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Error occurred in page generating. Column:{0}. Message:{1}.", (cm==null) ? "" : cm.Classname, ex.Message));
        }
    }

    // 所有首页生成
    protected void All_Index_Btn_click(object sender, EventArgs e)
    {
        ColumnModel cm = null;
        try
        {
            this.PopedomValidate(Layerid, PUB);
            start();
            int webid = int.Parse(Request.Params["webid"]);
            MenuModel menuModel = new MenuModel();
            IList datalist = menuModel.GetLayeridColumn(webid);
            CmsArticleBll acbll = new CmsArticleBll();
            datalist.Add(menuModel.GetColumn(webid));
            for (int i = 0; i < datalist.Count; i++)
            {
                cm = (ColumnModel)datalist[i];
                string dir = menuModel.GetLayerDir(cm.Layerid);
                acbll.CreateColumnIndexHtml(cm, dir);
            }
            end();
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Error occurred in page generating. Column:{0}. Message:{1}.", (cm == null) ? "" : cm.Classname, ex.Message));
        }
    }

    // 栏目首页生成
    protected void Index_Btn_click(object sender, EventArgs e)
    {
        try
        {
            this.PopedomValidate(Layerid, PUB);
            start();
            string id = ((LinkButton)sender).CommandArgument;
            int cid = int.Parse(id);
            MenuModel menuModel = new MenuModel(cid);
            CmsArticleBll acbll = new CmsArticleBll();
            string dir = menuModel.GetLayerDir(menuModel.GetColumn().Layerid);
            acbll.CreateColumnIndexHtml(menuModel.GetColumn(), dir);
            end();
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Error occurred in page generating. Message:{0}.", ex.Message));
        }
    }

    // 所有子栏目内容生成
    protected void Content_Btn_click(object sender, EventArgs e)
    {
        ColumnModel cm = null;
        try
        {
            this.PopedomValidate(Layerid, PUB);
            start();
            string id = ((LinkButton)sender).CommandArgument;
            int cid = int.Parse(id);
            MenuModel menuModel = new MenuModel(cid);
            IList clist = menuModel.GetLayeridColumn(cid);
            for (int m = 0; m < clist.Count; m++)
            {
                cm = (ColumnModel)clist[m];
                cid = cm.Classid;
                GenerateHtmlByColumnID(cid);
            }
            end();
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Error occurred in page generating. Column:{0}. Message:{1}.", (cm == null) ? "" : cm.Classname, ex.Message));
        }
    }

    private void GenerateHtmlByColumnID(int columnID)
    {
        IList<IStaticGeneratableModel> models = null;
        IGenerateHtml bll = null;
        ColumnModel column = null;
        try
        {
            MenuModel menuModel = new MenuModel(columnID);
            CmsArticleBll acbll = new CmsArticleBll();
            column = menuModel.GetColumn();
            string dir = menuModel.GetLayerDir(column.Layerid);
            
            bll = GenerateHtmlFactory.CreateInstance(columnID, this.LangID);
            if (bll != null)
            {
                models = bll.GetAllStaticPublishModels(columnID);
                foreach (IStaticGeneratableModel model in models)
                {
                    bll.GenerateHtml(model, dir);
                }
            }
            else
            {
                IList alist = acbll.GetColumnAllPubArticleList(columnID);
                for (int a = 0; a < alist.Count; a++)
                {
                    CmsArticleModel cam = (CmsArticleModel)alist[a];
                    acbll.CreateArticleHtml(cam, dir);
                }
            }
        }
        catch(Exception ex)
        {
            throw new Exception(string.Format("Error occurred in page GenerateHtmlByColumnID. Column:{0}. Message:{1}.", (column == null) ? "" : column.Classid + "-" + column.Classname, ex.Message));
        }
    }

    // 本栏目内容生成
    protected void And_Content_Btn_click(object sender, EventArgs e)
    {
        try
        {
            this.PopedomValidate(Layerid, PUB);
            start();
            string id = ((LinkButton)sender).CommandArgument;
            int cid = int.Parse(id);
            GenerateHtmlByColumnID(cid);
            IndexFiles();

            end();
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Error occurred in page generating. Message:{0}.", ex.Message));
        }
    }

    private void IndexFiles()
    {
        int webid = int.Parse(Request.Params["langId"]);
        string stype = "cn";
        if (webid == 2052)
            stype = "cn";
        else if (webid == 1033)
            stype = "en";
        
        try
        {
            LuceneBll luceneBll = new LuceneBll();
            luceneBll.DeleteLuceneFolder(Server.MapPath("~/" + stype + "/index2"));
            IGenerateHtml bll = new EventsBLL(this.LangID);
            luceneBll.CreateFolderIndex(Server.MapPath("~/" + stype + "/index2"), bll.CreateLuceneDocuments(), stype);
            //bll = new SponsorBLL(this.LangID);
            //luceneBll.CreateFolderIndex(Server.MapPath("~/" + stype + "/index2"), bll.CreateLuceneDocuments(), stype);

            ConfigXmlDocument cxDoc = new ConfigXmlDocument();
            cxDoc.Load(Server.MapPath("~/" + stype + "static.config"));
            System.Xml.XmlNodeList nodeList = cxDoc.SelectNodes("/configuration/appSettings/add");
            foreach (System.Xml.XmlNode node in nodeList)
            {
                string fileContent = String.Empty;
                string filePath = Server.MapPath(node.Attributes["key"].InnerText);
                if (!File.Exists(filePath)) continue;

                using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
                    fileContent = sr.ReadToEnd();

                luceneBll.CreateFileIndex(Server.MapPath("~/" + stype + "/index2"), node.Attributes["key"].InnerText, node.Attributes["value"].InnerText, fileContent);
            }
        }
        catch(Exception ex)
        {
            throw new Exception("<br/>生成索引文件时发生错误。 错误信息：" + ex.Message);
        }
    }

    private void start()
    {
        //this._listpage.Visible = false;
        //this.wait2.Visible = true;
    }
    private void end()
    {
        //this._listpage.Visible = true;
        //this.wait2.Visible = false;
    }
    protected void lbCreateIndex_click(object sender, EventArgs e)
    {
        IndexFiles();
    }
}
